package com.itqf.dao.impl;

import com.itqf.connecter.DbConnection;
import com.itqf.dao.GoodsDao;
import com.itqf.dao.TypeDao;
import com.itqf.entity.Goods;
import com.itqf.entity.Type;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class GoodsDaoImpl implements GoodsDao {
    @Override
    public long selectCountByTid(int tid) throws SQLException {
        String sql = "select count(1) from goods where t_id =?";
        Object result = null ;
        Connection conn=null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        int rows = 0;
        conn= DbConnection.getConnection();
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1, tid);
            rs = ps.executeQuery();
            while(rs.next()) {
              result = rs.getLong(1);
            }
        }catch(Exception e) {
            e.printStackTrace();
        }
        DbConnection.close(rs, ps);
        long total = (long)result;
        return total;
    }

    @Override
    public List<Goods> selectGoodsByPage(int page, int pageSize, int tid) throws SQLException {
        String sql = "select g_id as gid , t_id as tid , g_name as gname , g_time as gtime , g_img as img , g_state as gstate , g_info as ginfo , g_price as gprice from goods where t_id = ? limit ?,?;";
        List<Goods> list = new ArrayList<Goods>();
        Connection conn=null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        int rows = 0;
        conn= DbConnection.getConnection();
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1, tid);
            ps.setInt(2, (page-1)*pageSize);
            ps.setInt(3, pageSize);
            rs = ps.executeQuery();
            while(rs.next()) {
                int gid = rs.getInt(1);
                TypeDao typeDao = new TypeDaoImpl();
                Type type = typeDao.selectTypeByTid(tid);
                String gname = rs.getString(3);
                java.util.Date gtime = new java.util.Date(rs.getDate(4).getTime());
                String img = rs.getString(5);
                int gstate = rs.getInt(6);
                String ginfo = rs.getString(7);
                BigDecimal gprice = rs.getBigDecimal(8);
                Goods goods = new Goods(gid,tid,type,gname, gtime,img, gprice,gstate,ginfo);
                System.out.println(goods.toString());
                list.add(goods);
            }
        }catch(Exception e) {
            e.printStackTrace();
        }
        DbConnection.close(rs, ps);
        return list;
    }


    @Override
    public Goods selectGoodsByGid(int gid) throws SQLException {
        String sql = "select g_id as gid , t_id as tid , g_name as gname , g_time as gtime , g_img as img , g_state as gstate , g_info as ginfo , g_price as gprice from goods where g_id = ? ";
        Goods goods = null;
        Connection conn=null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        int rows = 0;
        conn= DbConnection.getConnection();
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1, gid);
            rs = ps.executeQuery();
            while(rs.next()) {
                int tid =  rs.getInt(2);
                TypeDao typeDao = new TypeDaoImpl();
                Type type = typeDao.selectTypeByTid(tid);
                String gname = rs.getString(3);
                java.util.Date gtime = new java.util.Date(rs.getDate(4).getTime());
                String img = rs.getString(5);
                int gstate = rs.getInt(6);
                String ginfo = rs.getString(7);
                BigDecimal gprice = rs.getBigDecimal(8);
                goods = new Goods(gid,tid,type,gname, gtime,img, gprice,gstate,ginfo);
            }
        }catch(Exception e) {
            e.printStackTrace();
        }
        DbConnection.close(rs, ps);
        return goods;
    }
}
